# encoding=utf8
import sys
import time
import math
reload(sys)
sys.setdefaultencoding('utf8')

class JT_0700:
    LatStrInfo = ""
    LongStrInfo = ""
    skr=""
    def AnalysisLat(self, source):
        # double Lat;
        try:
            Lat = int(source, 16) * math.pow(10, -6);
            # self.LatStrInfo = String.format("%.6f", Lat);
            self.LatStrInfo = "%.6f" % Lat
        except:
            pass

    def AnalysisLong(self, source):
        # double Long;
        try:
            Long = int(source, 16) * math.pow(10, -6);
            # self.LongStrInfo = String.format("%.6f", Long);
            self.LongStrInfo = "%.6f" % Long
        except:
            pass

    def LogicAisle(self, protocolStr, header):
     result=[]
     count=0
     t=0
     s = protocolStr[count:count + 4]
     header.append("应答流水号:" + s+"\n")
     count = count + 4
     s = protocolStr[count:count + 2]
     header.append("命令字:"+s)
     count=count+2
     s = protocolStr[count+2:count + 4]
     if s=="7e":
         pass
     else:
         s = protocolStr[count:count + 2]
         header.append("起始字头:" + s+"\n")
         count = count + 2
         s = protocolStr[count:count + 2]
         header.append("起始字头:" + s + "\n")
         count = count + 2
         s = protocolStr[count:count + 2]
         skr=s
         header.append("命令字:" + s + "\n")
         count = count + 2

         s = protocolStr[count:count + 4]
         print(s)
         n=int(s,16)
         header.append("数据块长度:" + str(n) + "\n")
         count = count + 4

         s = protocolStr[count:count + 2]
         header.append("保留（备用）字 :" + s + "\n")
         count = count + 2

         header.append("数据块:" )
         if skr=="00":
             header.append(s+"采集记录仪执行标准版本" + "\n")
             s = protocolStr[count:count + 2]
             header.append("记录仪执行标准年号后 2 位:" + s + "\n")
             count = count + 2
             s = protocolStr[count:count + 2]
             header.append("修改单号 :" + s + "\n")
             count = count + 2
         elif skr=="01":
             header.append(s + "采集当前驾驶人信息" + "\n")
             s = protocolStr[count:count + 36]
             header.append("机动车驾驶证号码:" + s + "\n")
             count = count + 36
         elif skr=="02":
             header.append(s + "采集记录仪实时时间" + "\n")
             s = protocolStr[count:count + 12]
             c = 0
             t = s[c:c + 2]
             header.append("时间：" + t)
             c = c + 2
             for i in range(5):
                 t = s[c:c + 2]
                 header.append("-" + t)
                 c = c + 2
             header.append("\n")
             count = count + 12
         elif skr=="03":
             header.append(s + "采集累计行驶里程 " + "\n")
             s = protocolStr[count:count + 12]
             c = 0
             t = s[c:c + 2]
             header.append("记录仪实时时间：" + t)
             c = c + 2
             for i in range(5):
                 t = s[c:c + 2]
                 header.append("-" + t)
                 c = c + 2
             header.append("\n")
             count = count + 12
             s = protocolStr[count:count + 12]
             c = 0
             t = s[c:c + 2]
             header.append("记录仪初次安装时间：" + t)
             c = c + 2
             for i in range(5):
                 t = s[c:c + 2]
                 header.append("-" + t)
                 c = c + 2
             header.append("\n")
             count = count + 12
             s = protocolStr[count:count + 8]
             n=int(s,16)
             header.append("初始里程:" + str(n) + "\n")
             count = count + 8
             s = protocolStr[count:count + 8]
             n = int(s, 16)
             header.append("累计行驶里程 :" + str(n) + "\n")
             count = count + 8
         elif skr=="04":
             header.append(s + "采集记录仪脉冲系数" + "\n")
             s = protocolStr[count:count + 12]
             c = 0
             t = s[c:c + 2]
             header.append("记录仪初次安装时间：" + t)
             c = c + 2
             for i in range(5):
                 t = s[c:c + 2]
                 header.append("-" + t)
                 c = c + 2
             header.append("\n")
             count = count + 12
             s = protocolStr[count:count + 2]
             header.append("记录仪脉冲系数高字节:" + s + "\n")
             count = count + 2
             s = protocolStr[count:count + 2]
             header.append("记录仪脉冲系数低字节:" + s + "\n")
             count = count + 2
         elif skr=="05":
             header.append(s + "采集车辆信息" + "\n")
             s = protocolStr[count:count + 34]
             header.append("车辆识别代号:" + s.decode("hex").decode('gb2312') + "\n")
             count = count + 34
             s = protocolStr[count:count + 24]
             header.append("机动车号牌号码:" + s.decode("hex").decode('gb2312') + "\n")
             count = count + 24
             s = protocolStr[count:count + 24]
             header.append("机动车号牌分类:" + s.decode("hex").decode('gb2312') + "\n")
             count = count + 24
         elif skr=="06":
             header.append(s + "采集记录仪状态信号配置信息" + "\n")
             s = protocolStr[count:count + 12]
             c = 0
             t = s[c:c + 2]
             header.append("记录仪实时时间：" + t)
             c = c + 2
             for i in range(5):
                 t = s[c:c + 2]
                 header.append("-" + t)
                 c = c + 2
             header.append("\n")
             count = count + 12
             s = protocolStr[count:count + 2]
             header.append("状态信号字节个数N:" + s + "\n")
             count = count + 2
             s = protocolStr[count:count + 160]
             header.append("第 1 字节状态信号配置:" + s + "\n")
             for i in range(8):
               t= s[c:c + 20]
               header.append("D"+str(i)+"的状态信号名称" + t.decode("hex").decode('gb2312'))
               c = c + 20
             count = count + 160
         elif skr=="07":
             header.append(s + "采集记录仪唯一性编号 " + "\n")
             s = protocolStr[count:count + 14]
             header.append("生产厂 CCC 认证代码:" + s.decode("hex").decode('gb2312') + "\n")
             count = count + 14
             s = protocolStr[count:count + 32]
             header.append("认证产品型号:" + s.decode("hex").decode('gb2312') + "\n")
             count = count + 32
             header.append("时间:")
             s = protocolStr[count:count + 2]
             header.append(  s +"年")
             count = count + 2
             s = protocolStr[count:count + 2]
             header.append(  s +"月")
             count = count + 2
             s = protocolStr[count:count + 2]
             header.append(  s +"日")
             count = count + 2
             s = protocolStr[count:count + 8]
             header.append("产品生产流水号" + s + "\n")
             count = count + 8
             s = protocolStr[count:count + 10]
             header.append("备用" + s + "\n")
             count = count + 10
         elif skr=="08":
             header.append(s + "采集指定的行驶速度记录" + "\n")
             s=protocolStr[count:-6]
             n=int(len(s)/252)
             count=0
             for i in range(n):
                 i1=i+1
                 header.append("===指定的结束时间之前最近的第"+str(i1)+" 分钟的行驶速度记录===\n")
                 st = s[count:count + 12]
                 c = 0
                 t = st[c:c + 2]
                 header.append("时间：" + t)
                 c = c + 2
                 for i in range(5):
                     t = st[c:c + 2]
                     header.append("-" + t)
                     c = c + 2
                 header.append("\n")
                 count = count + 12
                 for i in range(60):
                  st = s[count:count + 2]
                  speed=int(st,16)
                  ii=i+1
                  header.append("开始时间之后第 "+str(ii)+" 秒钟的平均速度:" + str(speed)+"\n")
                  count=count+2
                  st = s[count:count + 2]
                  ii = i + 1
                  header.append("开始时间之后第 " + str(ii) + "秒钟的状态信号:" + st+"\n")
                  count = count + 2
         elif skr== "09":
             header.append(s + "采集指定的行驶速度记录" + "\n")
             s = protocolStr[count:-6]
             n = int(len(s) / 1332)
             count = 0
             for i in range(n):
                 i1 = i + 1
                 header.append("===指定的结束时间之前最近的第" + str(i1) + " 小时的位置信息记录===\n")
                 st = s[count:count + 12]
                 c = 0
                 t = st[c:c + 2]
                 header.append("开始时间：" + t)
                 c = c + 2
                 for i in range(5):
                     t = st[c:c + 2]
                     header.append("-" + t)
                     c = c + 2
                 header.append("\n")
                 count = count + 12
                 for i in range(60):
                     ii = i + 1
                     header.append("===开始时间之后第 " + str(ii) + "分钟的位置信息:===\n")

                     st = s[count:count + 8]
                     self.AnalysisLong(st)
                     header.append( "经度"+ self.LongStrInfo + "\n")
                     count = count + 8
                     st = s[count:count + 8]
                     self.AnalysisLat(str)
                     header.append("纬度"+ self.LatStrInfo + "\n")
                     count = count+8
                     st = s[count:count + 4]
                     high = int(st, 16)
                     header.append("海拔:"+ str(high))
                     count = count + 4

                     st = s[count:count + 2]
                     speed=int(st,16)
                     header.append("开始时间之后第 " + str(ii) + "分钟的平均速度:" + str(speed)+"\n")
                     count = count + 2
         elif skr=="10":
             header.append(s + "采集指定的事故疑点记录:" + "\n")
             s = protocolStr[count:-6]
             n = int(len(s) / 468)
             count = 0
             for i in range(n):
                 i1 = i + 1
                 header.append("===指定的结束时间之前最近的第" + str(i1) + " 条事故疑点记录===\n")
                 st = s[count:count + 12]
                 c = 0
                 t = st[c:c + 2]
                 header.append("行驶结束时间：" + t)
                 c = c + 2
                 for i in range(5):
                     t = st[c:c + 2]
                     header.append("-" + t)
                     c = c + 2
                 header.append("\n")
                 count = count + 12
                 st = s[count:count + 36]
                 header.append("机动车驾驶证号码:" + st.decode("hex").decode('gb2312') + "\n")
                 count = count + 36
                 st = s[count:count + 2]
                 speed = int(st, 16)
                 header.append("行驶结束时的速度:" + str(speed) + "\n")
                 count = count + 2
                 st = s[count:count + 2]
                 header.append("行驶结束时的状态信号:" + st + "\n")
                 count = count + 2

                 for i in range(99):
                     ii = (i + 1)*0.2
                     st = s[count:count + 2]
                     speed = int(st, 16)
                     header.append("开始时间之后第 " + str(ii) + "秒时的速度:" + str(speed) + "\n")
                     count = count + 2
                     st = s[count:count + 2]
                     header.append("开始时间之后第 " + str(ii) + "秒时的状态信号:" + st + "\n")
                     count = count + 2

                 header.append("行驶结束时间前的最近一次有效位置信息 :")
                 st = s[count:count + 8]
                 self.AnalysisLong(st)
                 header.append("经度" + self.LongStrInfo + "\n")
                 count = count + 8
                 st = s[count:count + 8]
                 self.AnalysisLat(str)
                 header.append("纬度" + self.LatStrInfo + "\n")
                 count = count + 8
                 st = s[count:count + 4]
                 high = int(st, 16)
                 header.append("海拔:" + str(high)+"\n")
                 count = count + 4

         elif skr=="11":
             header.append(s + "采集指定的超时驾驶记录:" + "\n")
             s = protocolStr[count:-6]
             n = int(len(s) / 100)
             count = 0
             for i in range(n):
                 i1 = i + 1
                 header.append("===指定的结束时间之前最近的第" + str(i1) + " 条超时驾驶记录===\n")
                 st = s[count:count + 36]
                 header.append("机动车驾驶证号码:" + st.decode("hex").decode('gb2312') + "\n")
                 count = count + 36

                 st = s[count:count + 12]
                 c = 0
                 t = st[c:c + 2]
                 header.append("连续驾驶开始时间:" + t)
                 c = c + 2
                 for i in range(5):
                     t = st[c:c + 2]
                     header.append("-" + t)
                     c = c + 2
                 header.append("\n")
                 count = count + 12

                 st = s[count:count + 12]
                 c = 0
                 t = st[c:c + 2]
                 header.append("连续驾驶结束时间:" + t)
                 c = c + 2
                 for i in range(5):
                     t = st[c:c + 2]
                     header.append("-" + t)
                     c = c + 2
                 header.append("\n")
                 count = count + 12

                 header.append("连续驾驶开始时间所在的最近一次有效位置信息  :")
                 st = s[count:count + 8]
                 self.AnalysisLong(st)
                 header.append("经度" + self.LongStrInfo + "\n")
                 count = count + 8
                 st = s[count:count + 8]
                 self.AnalysisLat(str)
                 header.append("纬度" + self.LatStrInfo + "\n")
                 count = count + 8
                 st = s[count:count + 4]
                 high = int(st, 16)
                 header.append("海拔:" + str(high) + "\n")
                 count = count + 4

                 header.append("连续驾驶结束时间所在的最近一次有效位置信息:")
                 st = s[count:count + 8]
                 self.AnalysisLong(st)
                 header.append("经度" + self.LongStrInfo + "\n")
                 count = count + 8
                 st = s[count:count + 8]
                 self.AnalysisLat(str)
                 header.append("纬度" + self.LatStrInfo + "\n")
                 count = count + 8
                 st = s[count:count + 4]
                 high = int(st, 16)
                 header.append("海拔:" + str(high) + "\n")
                 count = count + 4

         elif skr=="12":
             header.append(s + "采集指定的驾驶人身份记录:" + "\n")
             s = protocolStr[count:-6]
             n = int(len(s) / 50)
             count = 0
             for i in range(n):
                 i1 = i + 1
                 header.append("===指定的结束时间之前最近的第" + str(i1) + "条驾驶人登录退出记录===\n")
                 st = s[count:count + 12]
                 c = 0
                 t = st[c:c + 2]
                 header.append("事件发生时间:" + t)
                 c = c + 2
                 for i in range(5):
                     t = st[c:c + 2]
                     header.append("-" + t)
                     c = c + 2
                 header.append("\n")
                 count = count + 12
                 st = s[count:count + 36]
                 header.append("机动车驾驶证号码:" + st.decode("hex").decode('gb2312') + "\n")
                 count = count + 36
                 st = s[count:count + 2]
                 header.append("事件类型:"+st )
                 if st=="01":
                   header.append("登陆" + "\n")
                   count = count + 2
                 elif st=="02":
                    header.append("退出" + "\n")
                    count = count + 2

         elif skr== "13":
             header.append(s + "采集指定的记录仪外部供电记录:" + "\n")
             s = protocolStr[count:-6]
             n = int(len(s) / 14)
             count = 0
             for i in range(n):
                 i1 = i + 1
                 header.append("===指定的结束时间之前最近的第" + str(i1) + "条外部电源记录===\n")
                 st = s[count:count + 12]
                 c = 0
                 t = st[c:c + 2]
                 header.append("事件发生时间:" + t)
                 c = c + 2
                 for i in range(5):
                     t = st[c:c + 2]
                     header.append("-" + t)
                     c = c + 2
                 header.append("\n")
                 count = count + 12
                 st = s[count:count + 2]
                 header.append("事件类型:" + st)
                 if st == "01":
                     header.append("通电" + "\n")
                     count = count + 2
                 elif st == "02":
                     header.append("断电" + "\n")
                     count = count + 2
         elif skr== "14":
             header.append(s + "采集指定的记录仪参数修改记录:" + "\n")
             s = protocolStr[count:-6]
             n = int(len(s) / 14)
             count = 0
             for i in range(n):
                 i1 = i + 1
                 header.append("===指定的结束时间之前最近的第" + str(i1) + " 条参数修改记录===\n")
                 st = s[count:count + 12]
                 c = 0
                 t = st[c:c + 2]
                 header.append("事件发生时间:" + t)
                 c = c + 2
                 for i in range(5):
                     t = st[c:c + 2]
                     header.append("-" + t)
                     c = c + 2
                 header.append("\n")
                 count = count + 12
                 st = s[count:count + 2]
                 header.append("事件类型:" + st)
                 count = count + 2
         elif skr== "15":
             header.append(s + "采集指定的速度状态日志\n")
             s = protocolStr[count:-6]
             s_len = [1, 6, 6, 1, 1, 1, 1, 1, 1]
             while len(s) >= 133:
                 data_str = s[0:133]
                 s = s[133:]
                 status = data_str[0:2]
                 if status == '01':
                     header.append('记录仪的速度状态：01 正常\n')
                 elif status == '02':
                     header.append('记录仪的速度状态：02 正常\n')
                 time_start = data_str[2:14]
                 c = 0
                 t = time_start[c:c + 2]
                 header.append("速度状态判定的时间：" + t)
                 c = c + 2
                 for i in range(5):
                     t = time_start[c:c + 2]
                     header.append("-" + t)
                     c = c + 2
                 header.append('\n')
                 time_terminal = data_str[14:26]
                 c = 0
                 t = time_terminal[c:c + 2]
                 header.append("时间：" + t)
                 c = c + 2
                 for i in range(5):
                     t = time_terminal[c:c + 2]
                     header.append("-" + t)
                     c = c + 2
                 header.append('\n')
                 sudu = data_str[26:]
                 c = 0
                 for i in range(59):
                     sudu_str = sudu[c:c + 4]
                     c += 4
                     header.append('开始时间后第' + str(i + 1) + '秒对应的记录速度：' + sudu_str[0:2] + '\n')
                     header.append('开始时间第' + str(i + 1) + '秒对应的参考速度：' + sudu_str[2:4] + '\n')
     s = protocolStr[-6:-4]
     header.append("校验字："+s)
     self.result = header

